package fr.unice.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import fr.unice.client.AudioFile;
import fr.unice.client.Playlist;
import fr.unice.client.services.InitializationService;

@SuppressWarnings("serial")
public class InitializationServiceImpl extends RemoteServiceServlet implements
		InitializationService {
	private static final String[][] audioFiles = {
			{ "O Na Som", "D Kin (feat. J Dess)", "The Arrival", "2009", "0",
					"Hip Dance", "00:54",
					"http://bst-player.googlecode.com/svn/tags/showcase/060/media/o-na-som.mp3" },
			{ "Cesaria", "Cesária Évora", "Cesaria Evora Enthology", "2006",
					"1", "Morna", "00:22",
					"http://www.alsacreations.fr/mediaflash/mp3/cesaria.mp3" },
			{ "Everywhere", "Michelle Branch", "Master Blaster", "2003", "1",
					"Pop", "00:20",
					"http://www.alsacreations.fr/mediaflash/mp3/everywhere.mp3" },
			{ "La Moldau", "Smetana", "Smetana : La Moldau", "2000", "1",
					"Classic", "00:32",
					"http://www.alsacreations.fr/mediaflash/mp3/moldau.mp3" } };

	private static final String[] playlists = { "Rock'n roll", "So Cool",
			"Web Attitude" };

	public void initAudioFiles() {
		ArrayList<AudioFile> audioFileObjects = new ArrayList<AudioFile>(
				InitializationServiceImpl.audioFiles.length);
		for (String[] audioFile : InitializationServiceImpl.audioFiles) {
			audioFileObjects.add(new AudioFile(audioFile[0], audioFile[1],
					audioFile[2], audioFile[3], audioFile[4], audioFile[5],
					audioFile[6], audioFile[7]));
		}

		PersistenceManager pm = this.getPersistenceManager();
		try {
			pm.makePersistentAll(audioFileObjects);
		} finally {
			pm.close();
		}
	}

	/*
	 * public void initAudioFiles(String uri, String[] fields) {
	 * 
	 * System.out.println("Listing files from " + uri);
	 * 
	 * ArrayList<AudioFile> songs = new ArrayList<AudioFile>(); File dir = new
	 * File(uri);
	 * 
	 * File[] files = dir.listFiles();
	 * 
	 * for (File file : files) { if (file.isDirectory()) { File[] sousRepertoire
	 * = file.listFiles();
	 * 
	 * for (File element : sousRepertoire) { //
	 * songs.add(this.initAudioFiles(element.getAbsolutePath(), // fields)); } }
	 * else { String[] ext = file.getName().split("."); if (ext[ext.length -
	 * 1].equals("mp3")) { try { ID3v1 tags = new MP3File(file).getID3v1Tag();
	 * songs.add(new AudioFile(tags.getSongTitle(), tags .getArtist(),
	 * tags.getAlbum(), tags.getYear(), tags.getTrackNumberOnAlbum(), null, tags
	 * .getSize()));
	 * 
	 * } catch (IOException e) { e.printStackTrace(); } catch (TagException e) {
	 * e.printStackTrace(); } } } } }
	 */

	public void initPlaylists() {
		ArrayList<Playlist> playlistObjects = new ArrayList<Playlist>(
				InitializationServiceImpl.playlists.length);
		for (String playlist : InitializationServiceImpl.playlists) {
			playlistObjects.add(new Playlist(playlist));
		}

		PersistenceManager pm = this.getPersistenceManager();
		try {
			pm.makePersistentAll(playlistObjects);
		} finally {
			pm.close();
		}
	}

	public void deleteAll() {
		List<AudioFile> audioFiles;
		List<Playlist> playlists;
		Query q = null;
		PersistenceManager pm = null;

		pm = PMF.getPersistenceManager();
		try {
			q = pm.newQuery(Playlist.class);
			playlists = (List<Playlist>) q.execute();
			playlists = (List<Playlist>) pm.detachCopyAll(playlists);
		} finally {
			pm.close();
		}

		pm = PMF.getPersistenceManager();
		try {
			q = pm.newQuery(AudioFile.class);
			audioFiles = (List<AudioFile>) q.execute();
			audioFiles = (List<AudioFile>) pm.detachCopyAll(audioFiles);
		} finally {
			pm.close();
		}

		pm = PMF.getPersistenceManager();
		try {
			pm.deletePersistentAll(playlists);
			pm.deletePersistentAll(audioFiles);
		} finally {
			pm.close();
		}
	}

	public PersistenceManager getPersistenceManager() {
		return PMF.get().getPersistenceManager();
	}
}
